package cs.android.view;

import static cs.java.lang.Lang.is;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import cs.android.ActivityView;
import cs.android.viewbase.ActivityViewBase;

public class ActivityViewDialog extends ActivityViewBase {

	private Dialog dialog;
	private ActivityView view;

	public ActivityViewDialog(ActivityView hasActivity) {
		super(hasActivity);
	}

	protected void onDialogDissmiss() {
		view.deinitialize(getState());
		dialog = null;
	}

	@Override protected void onPause() {
		super.onPause();
		hideDialog();
	}

	@Override protected void onResume() {
		super.onResume();
		if (is(view)) {
			view.initialize(getState());
			showDialog();
		}
	}

	private void showDialog() {
		dialog = new Dialog(getActivity());
		dialog.setOnDismissListener(new OnDismissListener() {
			public void onDismiss(DialogInterface dialog) {
				onDialogDissmiss();
			}
		});
		dialog.setContentView(view.asView());
		dialog.show();
	}

	public void hideDialog() {
		if (is(dialog)) dialog.dismiss();
	}

	public void show(ActivityView view) {
		this.view = view;
		view.initialize(null);
		showDialog();
	}

}
